<template>
  <el-dialog v-model="visible" :title="!dataForm.tenantId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="门店名称" prop="storeName">
        <el-input v-model="dataForm.storeName" placeholder="门店名称"></el-input>
      </el-form-item>
      <el-form-item label="门店描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="门店描述"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
      </el-form-item>
      <el-form-item label="门店地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="门店地址"></el-input>
      </el-form-item>
      <el-form-item label="营业时间" prop="operatingHours">
        <el-date-picker type="datetime" placeholder="营业时间" v-model="dataForm.operatingHours" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="门店评分" prop="rating">
        <el-input v-model="dataForm.rating" placeholder="门店评分"></el-input>
      </el-form-item>
      <el-form-item label="是否是总店" prop="isHeadStore">
        <el-radio-group v-model="dataForm.isHeadStore">
          <el-radio :label="1">总店</el-radio>
          <el-radio :label="0">分店</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 客户端 -->
      <el-form-item label="分配客户端">
        <el-select multiple v-model="dataForm.clientPrimaryIds" placeholder="分配客户端">
          <el-option :label="data.clientName" v-for="data in clientList" :key="data.id" :value="data.id">{{ data.clientName }}</el-option>
        </el-select>
      </el-form-item>

    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);
const clientList = ref();
const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  storeId: "",
  tenantId: "",
  clientPrimaryIds: [],
  storeName: "",
  description: "",
  latitude: "",
  longitude: "",
  address: "",
  isHeadStore: null,
  operatingHours: "",
  rating: "",
  creator: "",
  createDate: "",
  updator: "",
  updateDate: "",
});

const rules = ref({
  storeName: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  description: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  latitude: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  longitude: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  operatingHours: [{ required: true, message: t("validate.required"), trigger: "blur" }],
});

// 获取客户端信息
const getClientList = () => {
  baseService.post("/sys/oauth2/client/list", {}).then((res) => {
    if (res.code === 200) {
      clientList.value = res.data;
    }
  });
};

const init = (storeId?: number) => {
  visible.value = true;
  dataForm.storeId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  Promise.all([getClientList()]).then(() => {
    if (storeId) {
      getInfo(storeId);
    }
  });
};

// 获取信息
const getInfo = (storeId: number) => {
  baseService.get("/sys/sysstore/" + storeId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.storeId ? baseService.post : baseService.put)("/sys/sysstore", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>